package com.sc.justification.system;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import com.sc.justification.dao.GenericDao;
import com.sc.justification.model.JustificationCategory;
import com.sc.justification.model.JustificationTemplate;

public class TemplatePanel extends JPanel {
	private JTable table;
	private JDialog dialog;
	private GenericDao<JustificationCategory> dao=new GenericDao<JustificationCategory>(JustificationCategory.class);
	private List<JustificationCategory> categories=new ArrayList<JustificationCategory>();
	private List<JustificationTemplate> justificationTemplates=ObservableCollections.observableList(new ArrayList<JustificationTemplate>());
	private JList list;
	/**
	 * Create the panel.
	 */
	public TemplatePanel(JDialog dialog) {
		categories=dao.getAll();
		this.dialog=dialog;
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		
		JButton btnNewButton_5 = new JButton("New button");
		panel.add(btnNewButton_5);
		
		JButton btnNewButton_4 = new JButton("New button");
		panel.add(btnNewButton_4);
		
		JButton btnNewButton_3 = new JButton("New button");
		panel.add(btnNewButton_3);
		
		JButton btnNewButton_2 = new JButton("New button");
		panel.add(btnNewButton_2);
		
		JButton btnNewButton_1 = new JButton("New button");
		panel.add(btnNewButton_1);
		
		JSplitPane splitPane = new JSplitPane();
		add(splitPane, BorderLayout.CENTER);
		splitPane.setDividerLocation(200);
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setLeftComponent(scrollPane);
		
		list = new JList();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				JustificationCategory justificationCategory = categories.get(list.getSelectedIndex());
				dao.refresh(justificationCategory);
				justificationTemplates.clear();
				justificationTemplates.addAll(justificationCategory.getJustificationTemplates());
			}
		});
		scrollPane.setViewportView(list);
		
		JLabel lblNewLabel = new JLabel("鉴定类别");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		scrollPane.setColumnHeaderView(lblNewLabel);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		splitPane.setRightComponent(scrollPane_1);
		
		table = new JTable();
		scrollPane_1.setViewportView(table);
		initDataBindings();

	}
	protected void initDataBindings() {
		JListBinding<JustificationCategory, List<JustificationCategory>, JList> jListBinding = SwingBindings.createJListBinding(UpdateStrategy.READ_WRITE, categories, list);
		//
		BeanProperty<JustificationCategory, String> justificationCategoryBeanProperty = BeanProperty.create("name");
		jListBinding.setDetailBinding(justificationCategoryBeanProperty);
		//
		jListBinding.bind();
		//
		ELProperty<JList, JustificationCategory> jListBeanProperty = ELProperty.create("${selectedElement.justificationTemplates}");
		AutoBinding<List<JustificationTemplate>, List<JustificationTemplate>, JList, JustificationCategory> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, justificationTemplates, list, jListBeanProperty);
		autoBinding.bind();
		//
		JTableBinding<JustificationTemplate, List<JustificationTemplate>, JTable> jTableBinding = SwingBindings.createJTableBinding(UpdateStrategy.READ_WRITE, justificationTemplates, table);
		//
		BeanProperty<JustificationTemplate, String> justificationTemplateBeanProperty = BeanProperty.create("name");
		jTableBinding.addColumnBinding(justificationTemplateBeanProperty).setColumnName("\u9274\u5B9A\u6A21\u677F");
		//
		jTableBinding.bind();
	}
}
